# -*- coding: utf-8 -*-

import numpy as np


def ndarray():
    n = 10

    # 一维数组=向量
    a = np.array(range(n))
    print(type(a))  # numpy.ndarray
    print('a =\n', a)
    # 二维数组=矩阵
    a2 = np.arange(n * n).reshape(n, n)
    print(type(a2))
    print('a2 =\n', a2)

    # 取行
    print('a2[1] =\n', a2[1])
    # 取列
    print('a2[:, 5] =\n', a2[:, 5])
    # 行列
    print('a2[0:n:2, 0:n:2] =\n', a2[0:n:2, 0:n:2])
    # 元素
    print('a2[1][2] =\n', a2[1][2])  # 单个元素
    print('a2[[1, 2, 3], [3, 4, 5]] =\n', a2[[1, 2, 3], [3, 4, 5]])  # 多个元素：前后坐标个数相同
    pass


def slice():
    """切片"""
    a = np.array(range(10))
    print(a)
    print(a > 5)
    print(a[a > 5])
    pass


def size_shape():
    x = np.arange(0, 6)
    print('x:----------\n', x, sep='')

    a = x.reshape(2, 3)
    print('a:----------\n', a, sep='')

    x.resize(3, 2)
    print('x:----------\n', x, sep='')

    print('x.size', x.size)
    print('np.size(x)', np.size(x))
    print('len(x)', len(x))

    b = np.ravel(a=a)

    y = x.flatten()
    pass


def vector_multiply():
    """
    向量乘法
    :return:
    """
    a = np.arange(0, 3)
    b = np.arange(0, 3)

    # 点乘：前后形状相同
    c1 = a * b
    c2 = np.multiply(a, b)

    # 叉乘：前列等于后行
    c3 = a @ b
    c4 = np.dot(a, b)

def matrix_multiply():
    """
    矩阵乘法
    :return:
    """

    a = np.arange(0, 6).reshape(2, 3)
    b = np.arange(0, 6).reshape(2, 3)

    # 点乘：前后形状相同
    c1 = a * b
    c2 = np.multiply(a, b)

    # 叉乘：前列等于后行，转置后列数要一定相同，行数可以不相同
    c3 = a @ b.T
    c4 = np.dot(a, b.T)

    pass


def np_method():
    # 统计函数，设置按轴统计
    x = np.random.randint(0, 6, size=(2, 3))
    sumX = x.sum()  # 全部统计
    sum0 = x.sum(axis=0)  # 按列统计
    sum1 = x.sum(axis=1)  # 按行统计

    print('x:----------\n', x, sep='')
    print('sumX', sumX)
    print('sum0', sum0)
    print('sum1', sum1)

    pass


def choose():
    a1 = np.arange(10)
    print('a1\n', a1, sep='')
    # 随机取样（设置是否重复）
    a2 = np.random.choice(a1, size=10)
    print('a2\n', a2, sep='')
    a3 = np.random.choice(a1, size=10, replace=False)
    print('a3\n', a3, sep='')

    pass


if __name__ == '__main__':
    # ndarray()
    # slice()

    # size_shape()

    matrix_multiply()

    pass
